#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"
char* delete_dir(char path[100])
{	
	
	char result[100];
	if(root==NULL)
		{
		strcpy(result,"deletedir_FAILURE ");
		strcat(result,ERR_VFS_DELETEDIR_04);
		sprintf(str, "%s",result);
		return str;
		}	
	if(strcmp(path,"/home/")==0)
		{
		strcpy(result,"deletedir_FAILURE ");
		strcat(result,ERR_VFS_DELETEDIR_02);
		sprintf(str, "%s",result);
		return str;
		}
	///printf("inside fn");
	int flag, empty_flag, id, hashvalue;
	char filename[40];
	char path1[100], path2[100];
	strcpy(path1,path);
	strcpy(path2,path);
//displayBstTree(bstroot);

	if(path[strlen(path)-1]!='/')
		strcat(path,"/");

	id=bst_search(bstroot,path);
	if(id==-1)
		{
		strcpy(result,"deletedir_FAILURE ");
		strcat(result,ERR_VFS_DELETEDIR_01);
		sprintf(str, "%s",result);
		return str;
		}
//printf("%d: %s",id,fd[id].location_path);

	if(strcmp(fd[id].file_type, "folder")!=0)
	{
		strcpy(result,"deletedir_FAILURE ");
		strcat(result,ERR_VFS_DELETEDIR_01);
		sprintf(str, "%s",result);
		return str;	
	}

	//displayTree(root);
	flag= nary_delete(path1, root);	// modifies path parameter
	//printf("flag: %d",flag);

	if(flag==0)
	{
	 strcpy(result,"deletedir_FAILURE ");
		strcat(result,ERR_VFS_DELETEDIR_02);
		sprintf(str, "%s",result);
		return str;
	}
	else if(flag==2)
	{
	strcpy(result,"deletedir_FAILURE ");
		strcat(result,ERR_VFS_DELETEDIR_01);
		sprintf(str, "%s",result);
		return str;
	}
	else if (flag==1)
	{
	 bstroot= (bstnode *)bst_delete(bstroot,path);
	 //printf("bst deletion done");
	 int len = strlen(path);
	 char *fromLast = path + (len -1);
	 int i = 0;
	 while(*fromLast != '/') 
		fromLast --;
	 fromLast++;
	 //printf("folder: %s\n",fromLast);
	 strcpy(filename,fromLast);
	
	 hashvalue=hash(filename);	
	 hash_deletion(hashvalue, filename,path);
	
	 fl[id].a=0;
	 main_header_var->no_used_file_descriptors=main_header_var->no_used_file_descriptors-1;
	}
	
	return "deletedir_SUCCESS";
} 



char* delete_file(char path[100])
{
	char result[100];
	if(path==NULL)
		{
		strcpy(result,"removefile_FAILURE ");
		strcat(result,ERR_VFS_REMOVEFILE_00);
		sprintf(str, "%s",result);
		return str;
		}
	if(root==NULL)
		{
		strcpy(result,"removefile_FAILURE ");
		strcat(result,ERR_VFS_REMOVEFILE_02);
		sprintf(str, "%s",result);
		return str;
		}
int flag, empty_flag, id, hashvalue;
	char filename[40];
	char path1[100], path2[100];
	strcpy(path1,path);
	strcpy(path2,path);

//displayBstTree(bstroot);
	id=bst_search(bstroot,path);
	if(id==-1)
		{
		strcpy(result,"removefile_FAILURE ");
		strcat(result,ERR_VFS_REMOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
		}
	if(strcmp(fd[id].file_type, "folder")==0)
	{
	        strcpy(result,"removefile_FAILURE ");
		strcat(result,ERR_VFS_REMOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
	}

	flag= nary_delete(path1, root);	// modifies path parameter

	if(flag==0)
	{
	 strcpy(result,"removefile_FAILURE ");
		strcat(result,ERR_VFS_REMOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
	}
	else if(flag==2)
	{
	 strcpy(result,"removefile_FAILURE ");
		strcat(result,ERR_VFS_REMOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
	}
	else if (flag==1)
	{
	 bstroot= (bstnode *)bst_delete(bstroot,path);
	 
	 int len = strlen(path);
	 char *fromLast = path + (len -1);
	 int i = 0;
	 while(*fromLast != '/') 
		fromLast --;
	 fromLast++;
	 //printf("folder: %s\n",fromLast);
	 strcpy(filename,fromLast);
	
	 hashvalue=hash(filename);	
	 hash_deletion(hashvalue, filename,path);
	
	 fl[id].a=0;
	main_header_var->no_used_file_descriptors=main_header_var->no_used_file_descriptors-1;
	 
	}
	return "removefile_SUCCESS";
} 
